﻿// Airspeed Indicator - 空速表

#include "AirspeedIndicator.h"

int r = 250;
int X = 4 * r;//窗口长
int Y = 3 * r; //窗口宽
int x = 500;//盘的横坐标
int y = 400; //盘的纵坐标

void drawAI(int x0, int y0, int r0, double speed) {
	//printf("请输入表盘的大小规格（整形数）：");
	x = x0;
	y = y0;
	r = r0;



	//setinitmode(0, x, y);//(0,x,y)
	//setcaption("空速表");//窗口名称
	//initgraph(X, Y, INIT_RENDERMANUAL);//创建窗口
	//setbkcolor(WHITE);
	ege_enable_aa(true); //开启抗锯齿
	double angle = 180;
	//double radin =-90*PI/180+speed*1.4285*PI/180-9*PI/180/*(speed - 90) * 1.75 * PI / 180*/;
	double radin = -120 * PI / 180 + speed * 1.75 * PI / 180/*(speed - 90) * 1.75 * PI / 180*/;

	//for (; is_run(); delay_fps(30)) {
	//	cleardevice();
	draw_kuang();
	draw_framework();
	draw_pointer(radin);
	//shadow();
	screwAI();
	//if (kbhit()) { // 检测是否有按键被按下
	//	int key = getch(); // 获取按下的键值
	//	switch (key) {
	//	case 'W': // 上箭头键
	//		radin += 0.8 * PI / 180; // 增加角度
	//		break;
	//	case 'S': // 下箭头键
	//		radin -= 0.8 * PI / 180; // 减少角度
	//		break;
	//	case 'A': // 左箭头键
	//		radin -= 0.8 * PI / 180; // 逆时针旋转
	//		break;
	//	case 'D': // 右箭头键
	//		radin += 0.8 * PI / 180; // 顺时针旋转
	//		break;
	//	case 27:
	//		return 0;
	//	}
	//}
	//getch();
	/*if (radin > 2 * PI) radin -= 2 * PI;
	if (radin < 0) radin += 2 * PI;*/
	//}
	//closegraph();
	//return 0;
}

void draw_framework() {
	setcolor(EGEARGB(42, 42, 42, 42));
	setfillcolor(EGEARGB(42, 42, 42, 42));//填充的颜色

	ege_circle(x, y, r * 0.95);
	fillcircle(x, y, r * 0.95);//填充圆

	// 画圆环（白，绿，黄，红）
	//白
	setcolor(EGEARGB(255, 255, 255, 255));
	setfillcolor(EGEARGB(255, 255, 255, 255));
	ege_pie(x - r * 0.865, y - r * 0.865, r * 1.73, r * 1.73, -49, 76);
	ege_fillpie(x - r * 0.865, y - r * 0.865, r * 1.73, r * 1.73, -49, 76);
	setcolor(EGEARGB(255, 42, 42, 42));
	setfillcolor(EGEARGB(255, 42, 42, 42));
	ege_pie(x - r * 0.77, y - r * 0.77, 1.54 * r, 1.54 * r, -51, 79);
	ege_fillpie(x - r * 0.77, y - r * 0.77, 1.54 * r, 1.54 * r, -51, 79);
	//绿
	setcolor(EGEARGB(255, 49, 215, 46));
	setfillcolor(EGEARGB(255, 49, 215, 46));
	ege_pie(x - r * 0.77, y - r * 0.77, 1.54 * r, 1.54 * r, -35, 161);
	ege_fillpie(x - r * 0.77, y - r * 0.77, 1.54 * r, 1.54 * r, -35, 161);
	setcolor(EGEARGB(255, 42, 42, 42));
	setfillcolor(EGEARGB(255, 42, 42, 42));
	ege_pie(x - r * 0.67, y - r * 0.67, 1.34 * r, 1.34 * r, -39, 166);
	ege_fillpie(x - r * 0.67, y - r * 0.67, 1.34 * r, 1.34 * r, -39, 166);
	//黄
	setcolor(EGEARGB(255, 244, 231, 70));
	setfillcolor(EGEARGB(255, 244, 231, 70));
	ege_pie(x - r * 0.77, y - r * 0.77, 1.54 * r, 1.54 * r, 126, 58);
	ege_fillpie(x - r * 0.77, y - r * 0.77, 1.54 * r, 1.54 * r, 126, 58);
	setcolor(EGEARGB(255, 42, 42, 42));
	setfillcolor(EGEARGB(255, 42, 42, 42));
	ege_pie(x - r * 0.67, y - r * 0.67, 1.34 * r, 1.34 * r, 124, 62);
	ege_fillpie(x - r * 0.67, y - r * 0.67, 1.34 * r, 1.34 * r, 124, 62);

	setcolor(RED);
	setlinewidth(7.5 * r / 250.0);
	ege_line(x - r * 0.67, y - r * 0.06, x - r * 0.94, y - r * 0.085);

	setcolor(WHITE);//刻度颜色
	//绘制刻度
	int numTicks = 17; // 刻度数量
	double angleStep = 280.0 / (numTicks - 1); // 每个刻度之间的角度
	double startAngle = 310; // 起始角度
	//画粗刻度
	for (int i = 0; i < numTicks; i++)
	{
		double angle = startAngle + i * angleStep; // 当前刻度的角度
		double radian = angle * PI / 180.0;     // 角度转弧度

		// 计算刻度线的起点和终点
		double startX = x + (r * 0.68 * cos(radian));
		double startY = y + (r * 0.68 * sin(radian));
		double endX = x + (r * 0.95 * cos(radian));
		double endY = y + (r * 0.95 * sin(radian));

		// 绘制刻度线
		setlinewidth(6 * r / 250.0);
		ege_line(startX, startY, endX, endY);
	}

	numTicks = 16; // 刻度数量
	angleStep = 280.0 / numTicks; // 每个刻度之间的角度
	startAngle = 310 + 135 / 15.8; // 起始角度
	//画细刻度
	for (int i = 0; i < numTicks; i++)
	{
		if (i == 13) continue;
		double angle = startAngle + i * angleStep; // 当前刻度的角度
		double radian = angle * PI / 180.0;     // 角度转弧度

		// 计算刻度线的起点和终点
		double startX = x + (r * 0.73 * cos(radian));
		double startY = y + (r * 0.73 * sin(radian));
		double endX = x + (r * 0.95 * cos(radian));
		double endY = y + (r * 0.95 * sin(radian));

		// 绘制刻度线
		setlinewidth(4 * r / 250.0);
		ege_line(startX, startY, endX, endY);
	}

	//灰色
	//setlinewidth(10);
	//setcolor(WHITE);
	//arcf(x, y,1.5 * r, 0*PI/180.0,90*PI/180.0);
	//画黑色的刻度

	//绘制刻度的变量
	setbkmode(TRANSPARENT);//透明字体背景
	setfont(r / 9, 0, "Arial");//字体

	settextcolor(WHITE);
	ege_outtextxy(x - r / 4.8, y - r / 5, "AIRSPEED");
	ege_outtextxy(x - r / 7, y + r / 4.5, "KNOTS");
	setfont(r / 12, 0, "Arial");
	ege_outtextxy(x - r / 20, y + r / 1.7, "TAS");
	setfont(r / 12, 0, "Arial");
	ege_outtextxy(x - r / 9, y - r / 2, "PRESS");
	ege_outtextxy(x - r / 20, y - r / 2.5, "ALT");

	ege_outtextxy(x - r / 6.5, y - r / 1.05, "T");
	ege_outtextxy(x - r / 9, y - r / 1.04, "E");
	ege_outtextxy(x - r / 15, y - r / 1.04, "M");
	ege_outtextxy(x - r / 250, y - r / 1.04, "P");
	ege_circle(x + r / 15, y - r / 1.05, r / 250);
	ege_outtextxy(x + r / 12, y - r / 1.045, "C");


	//绘制数字
	setfont(r / 5, 0, "Arial");
	ege_outtextxy(x + r / 3.2, y - r / 2.4, "40");
	ege_outtextxy(x + r / 2, y - r / 5.3, "60");
	ege_outtextxy(x + r / 2.5, y + r / 7, "80");
	ege_outtextxy(x + r / 5.4, y + r / 2.8, "100");

	ege_outtextxy(x - r / 4.8, y + r / 2.4, "120");
	ege_outtextxy(x - r / 2, y + r / 2.8, "140");
	ege_outtextxy(x - r / 1.6, y + r / 8.2, "160");
	ege_outtextxy(x - r / 1.6, y - r / 5, "180");
	ege_outtextxy(x - r / 2.2, y - r / 2, "200");

	//灰色下
	setlinewidth(40 * r / 250.0);
	setcolor(EGEARGB(255, 152, 152, 152));
	//x - r * 0.95, y - r * 0.95, 1.9 * r, 1.9 * r, 57, 68
	ege_arc(x - r * 0.88, y - r * 0.88, 1.76 * r, 1.76 * r, 56, 69);
	//灰色上
	setcolor(EGEARGB(255, 180, 180, 180));
	ege_arc(x - r * 0.65, y - r * 0.65, 1.3 * r, 1.3 * r, -103, 25);
	//中间的螺丝

}
void draw_pointer(double radin) {
	if (radin < -90 * PI / 180) {
		radin = -90 * PI / 180;
	}
	setfillcolor(WHITE);
	/*ege_circle(x, y, r / 25);
	ege_fillcircle(x, y, r / 25);*/
	ege_point polypoint[] = { x + r / 1.3 * cos(radin) ,y + r / 1.3 * sin(radin), x + r / 8 * cos(radin - 150 * PI / 180),y + r / 8 * sin(radin - 150 * PI / 180), x + r / 6 * cos(radin - 180 * PI / 180), y + r / 6 * sin(radin - 180 * PI / 180), x + r / 8 * cos(radin - 210 * PI / 180),y + r / 8 * sin(radin - 210 * PI / 180) };
	ege_fillpoly(4, polypoint);
	setfillcolor(GRAY);
	ege_point polypoint1[] = { x + r / 8 * cos(radin - 150 * PI / 180),y + r / 8 * sin(radin - 150 * PI / 180),
		x + r / 6 * cos(radin - 180 * PI / 180), y + r / 6 * sin(radin - 180 * PI / 180),
		x + r / 8 * cos(radin - 210 * PI / 180),y + r / 8 * sin(radin - 210 * PI / 180),
		x + r / 6.8 * cos(radin - 210 * PI / 180),y + r / 6.8 * sin(radin - 210 * PI / 180),
		x + r / 5 * cos(radin - 180 * PI / 180), y + r / 5 * sin(radin - 180 * PI / 180),
		x + r / 6.8 * cos(radin - 150 * PI / 180),y + r / 6.8 * sin(radin - 150 * PI / 180), };
	ege_fillpoly(6, polypoint1);
}
void draw_kuang() {

	int circle_r = r * 1;
	int circle_x = x;
	int circle_y = y;

	setcolor(BLACK);//描边颜色
	setlinewidth(15 * r / 250.0);//线条粗细
	ege_circle(circle_x, circle_y, circle_r + circle_r / 10);//描边

	ege_point p5 = { circle_x - circle_r - circle_r / 10 + circle_r / 30 * 11 ,circle_y - circle_r - circle_r / 10 + circle_r / 30 * 11 };
	ege_setpattern_ellipsegradient(p5, EGERGB(160, 160, 160), circle_x - circle_r - circle_r / 10, circle_y - circle_r - circle_r / 10, 2 * (circle_r + circle_r / 10), 2 * (circle_r + circle_r / 10), EGEACOLOR(0xFF, 0x00161616));
	ege_fillrect(circle_x - circle_r - circle_r / 10, circle_y - circle_r - circle_r / 10, 2 * (circle_r + circle_r / 10), 2 * (circle_r + circle_r / 10));

	//最外圈

	setcolor(BLACK);//描边颜色
	setlinewidth(2 * r / 250.0);//线条粗细
	ege_circle(circle_x, circle_y, circle_r + circle_r * 3 / 150);//描边

	ege_point p6 = { circle_x - circle_r - circle_r / 300 * 2 + circle_r / 30 * 9 ,circle_y - circle_r - circle_r / 300 * 2 + circle_r / 30 * 9 };
	ege_setpattern_ellipsegradient(p6, EGERGB(200, 200, 200), circle_x - circle_r - circle_r / 300 * 6, circle_y - circle_r - circle_r / 300 * 6, 2 * (circle_r + circle_r / 300 * 6), 2 * (circle_r + circle_r / 300 * 6), EGEACOLOR(0xFF, 0x003A3A3C));
	ege_fillrect(circle_x - circle_r - circle_r / 300 * 6, circle_y - circle_r - circle_r / 300 * 6, 2 * (circle_r + circle_r / 300 * 6), 2 * (circle_r + circle_r / 300 * 6));
	//第二圈

	setlinewidth(14 * r / 250.0);//线条粗细
	setfillcolor(EGERGB(50, 50, 50));//填充为灰色
	ege_circle(circle_x, circle_y, circle_r - circle_r / 30);//描边

	ege_point p7 = { circle_x  ,circle_y };
	ege_setpattern_ellipsegradient(p7, EGERGB(70, 70, 70), circle_x - circle_r + circle_r / 30, circle_y - circle_r + circle_r / 30, 2 * (circle_r - circle_r / 30), 2 * (circle_r - circle_r / 30), EGEACOLOR(0xFF, 0x00292925));
	ege_fillrect(circle_x - circle_r + circle_r / 30, circle_y - circle_r + circle_r / 30, 2 * (circle_r - circle_r / 30), 2 * (circle_r - circle_r / 30));


	//ege_fillcircle(circle_x, circle_y, circle_r - circle_r / 30);
	//setfillcolor(EGERGB(38, 38, 38));//填充为深灰色
//第三圈
}

void screwAI()//螺丝
{
	double DIAL_RADIUS = r;
	double CENTER_X = x;
	double CENTER_Y = y;

	// 使用float保证计算精度（根据需求可改为double）
	const float DIAL_RADIUS_F = (float)DIAL_RADIUS;
	const float CENTER_X_F = (float)CENTER_X;
	const float CENTER_Y_F = (float)CENTER_Y;

	// 参数化定义（精确到小数点后6位）
	const float SCREW_OFFSET = DIAL_RADIUS_F * 0.055555556f; // 1/18
	const float ELLIPSE_OFFSET_X = -DIAL_RADIUS_F * 0.111111111f; // -1/9
	const float ELLIPSE_OFFSET_Y = -DIAL_RADIUS_F * 0.166666667f; // -1/6
	const float ELLIPSE_SIZE = DIAL_RADIUS_F * 0.277777778f;  // 5/18
	const float RECT_OFFSET_X = DIAL_RADIUS_F * 0.222222222f;  // 2/9
	const float RECT_OFFSET_Y = DIAL_RADIUS_F * 0.333333333f;  // 1/3
	const float RECT_WIDTH = DIAL_RADIUS_F * 0.555555556f;   // 5/9
	const float RECT_HEIGHT = DIAL_RADIUS_F * 0.611111111f;  // 11/18
	const float CROSS_OFFSET1 = DIAL_RADIUS_F * 0.055555556f;  // 1/18
	const float CROSS_OFFSET2 = DIAL_RADIUS_F * 0.027777778f;  // 1/36
	const float LINE_WIDTH = DIAL_RADIUS_F * 0.016666667f;  // 1/60

	ege_enable_aa(TRUE);

	// 绘制函数（使用浮点坐标）
	auto draw_screw = [&](float cx, float cy) {
		// 精确到小数点后6位的渐变参数
		ege_setpattern_ellipsegradient(
			ege_point{ roundf(cx), roundf(cy) }, // 最终转整数
			EGEACOLOR(0xFF, BLACK),
			roundf(cx + ELLIPSE_OFFSET_X),
			roundf(cy + ELLIPSE_OFFSET_Y),
			roundf(ELLIPSE_SIZE),
			roundf(ELLIPSE_SIZE),
			EGEACOLOR(0, WHITE)
		);
		ege_fillrect(
			roundf(cx - RECT_OFFSET_X),
			roundf(cy - RECT_OFFSET_Y),
			roundf(RECT_WIDTH),
			roundf(RECT_HEIGHT)
		);

		// 灰色渐变层（精确尺寸调整）
		const float adjusted_size = ELLIPSE_SIZE - DIAL_RADIUS_F * 0.055555556f; // 替代原-10
		ege_setpattern_ellipsegradient(
			ege_point{
			  roundf(cx - SCREW_OFFSET * 0.5f),
				roundf(cy - SCREW_OFFSET * 0.5f)
			},
			EGEACOLOR(0xFF, EGERGB(170, 170, 170)),
			roundf(cx + ELLIPSE_OFFSET_X),
			roundf(cy + ELLIPSE_OFFSET_Y),
			roundf(adjusted_size),
			roundf(adjusted_size),
			EGEACOLOR(0xFF, EGERGB(20, 20, 20))
		);
		ege_fillrect(
			roundf(cx - RECT_OFFSET_X),
			roundf(cy - RECT_OFFSET_Y),
			roundf(RECT_WIDTH),
			roundf(RECT_HEIGHT)
		);

		// 十字线（亚像素级精度）
		setcolor(EGERGB(30, 30, 30));
		setlinewidth(roundf(LINE_WIDTH));

		const float cross_y = cy - CROSS_OFFSET2 * 2.0f;
		ege_line(
			roundf(cx - CROSS_OFFSET1), roundf(cross_y),
			roundf(cx + CROSS_OFFSET1), roundf(cross_y)
		);

		const float cross_x = cx;
		const float cross_y_start = cy + CROSS_OFFSET2 / 4;
		const float cross_y_end = cy - CROSS_OFFSET2 * 4.0f;
		ege_line(
			roundf(cross_x), roundf(cross_y_start),
			roundf(cross_x), roundf(cross_y_end)
		);
		};

	// 计算四个螺丝中心坐标（浮点运算）
	const float screw_radius = DIAL_RADIUS_F - SCREW_OFFSET;

	// 左上
	draw_screw(
		CENTER_X_F - screw_radius,
		CENTER_Y_F - DIAL_RADIUS_F
	);

	// 右上
	draw_screw(
		CENTER_X_F + screw_radius,
		CENTER_Y_F - DIAL_RADIUS_F
	);

	// 右下
	draw_screw(
		CENTER_X_F + screw_radius,
		CENTER_Y_F + DIAL_RADIUS_F
	);

	// 左下（新增）
	draw_screw(
		CENTER_X_F - screw_radius,
		CENTER_Y_F + DIAL_RADIUS_F
	);
}//螺丝
//void shadow()
//{
//	float CENTER_X = x;
//	float CENTER_Y = y;
//	float DIAL_RADIUS = r;
//	ege_enable_aa(TRUE);  // 启用抗锯齿
//	//左上
//	ege_setpattern_ellipsegradient(ege_point{ CENTER_X - DIAL_RADIUS + 10, CENTER_Y - DIAL_RADIUS }, EGEACOLOR(0xFF, BLACK), CENTER_X - DIAL_RADIUS - 10, CENTER_Y - DIAL_RADIUS - 30, 50, 50, EGEACOLOR(0, WHITE));
//	ege_fillrect(CENTER_X - DIAL_RADIUS - 40, CENTER_Y - DIAL_RADIUS - 60, CENTER_X - DIAL_RADIUS + 30 + 30, CENTER_Y - DIAL_RADIUS + 20 + 30);
//	ege_setpattern_ellipsegradient(ege_point{ CENTER_X - DIAL_RADIUS , CENTER_Y - DIAL_RADIUS - 10 }, EGEACOLOR(0xFF, EGERGB(170, 170, 170)), CENTER_X - DIAL_RADIUS - 10, CENTER_Y - DIAL_RADIUS - 30, 40, 40, EGEACOLOR(0xFF, EGERGB(20, 20, 20)));
//	ege_fillrect(CENTER_X - DIAL_RADIUS - 40, CENTER_Y - DIAL_RADIUS - 60, CENTER_X - DIAL_RADIUS + 30 + 30, CENTER_Y - DIAL_RADIUS + 20 + 30);
//	//十字
//	setcolor(EGERGB(30, 30, 30));
//	setlinewidth(3);
//	ege_line(CENTER_X - DIAL_RADIUS - 2, CENTER_Y - DIAL_RADIUS - 10, CENTER_X - DIAL_RADIUS + 18, CENTER_Y - DIAL_RADIUS - 10);
//	ege_line(CENTER_X - DIAL_RADIUS + 8, CENTER_Y - DIAL_RADIUS - 1, CENTER_X - DIAL_RADIUS + 8, CENTER_Y - DIAL_RADIUS - 19);
//	//右上
//	ege_setpattern_ellipsegradient(ege_point{ CENTER_X + DIAL_RADIUS , CENTER_Y - DIAL_RADIUS }, EGEACOLOR(0xFF, BLACK), CENTER_X + DIAL_RADIUS - 20, CENTER_Y - DIAL_RADIUS - 30, 50, 50, EGEACOLOR(0, WHITE));
//	ege_fillrect(CENTER_X + DIAL_RADIUS - 50, CENTER_Y - DIAL_RADIUS - 60, CENTER_X + DIAL_RADIUS + 30 + 20, CENTER_Y - DIAL_RADIUS + 20 + 30);
//	ege_setpattern_ellipsegradient(ege_point{ CENTER_X + DIAL_RADIUS - 10, CENTER_Y - DIAL_RADIUS - 10 }, EGEACOLOR(0xFF, EGERGB(170, 170, 170)), CENTER_X + DIAL_RADIUS - 20, CENTER_Y - DIAL_RADIUS - 30, 40, 40, EGEACOLOR(0xFF, EGERGB(20, 20, 20)));
//	ege_fillrect(CENTER_X + DIAL_RADIUS - 50, CENTER_Y - DIAL_RADIUS - 60, CENTER_X + DIAL_RADIUS + 30 + 20, CENTER_Y - DIAL_RADIUS + 20 + 30);
//	//十字
//	ege_line(CENTER_X + DIAL_RADIUS - 9, CENTER_Y - DIAL_RADIUS - 10, CENTER_X + DIAL_RADIUS + 11, CENTER_Y - DIAL_RADIUS - 10);
//	ege_line(CENTER_X + DIAL_RADIUS + 1, CENTER_Y - DIAL_RADIUS - 1, CENTER_X + DIAL_RADIUS + 1, CENTER_Y - DIAL_RADIUS - 19);
//	//右下
//	ege_setpattern_ellipsegradient(ege_point{ CENTER_X + DIAL_RADIUS , CENTER_Y + DIAL_RADIUS }, EGEACOLOR(0xFF, BLACK), CENTER_X + DIAL_RADIUS - 20, CENTER_Y + DIAL_RADIUS - 30, 50, 50, EGEACOLOR(0, WHITE));
//	ege_fillrect(CENTER_X + DIAL_RADIUS - 50, CENTER_Y + DIAL_RADIUS - 60, CENTER_X + DIAL_RADIUS + 30 + 20, CENTER_Y + DIAL_RADIUS + 20 + 30);
//	ege_setpattern_ellipsegradient(ege_point{ CENTER_X + DIAL_RADIUS - 10, CENTER_Y + DIAL_RADIUS }, EGEACOLOR(0xFF, EGERGB(170, 170, 170)), CENTER_X + DIAL_RADIUS - 20, CENTER_Y + DIAL_RADIUS - 30, 40, 40, EGEACOLOR(0xFF, EGERGB(20, 20, 20)));
//	ege_fillrect(CENTER_X + DIAL_RADIUS - 50, CENTER_Y + DIAL_RADIUS - 60, CENTER_X + DIAL_RADIUS + 30 + 20, CENTER_Y + DIAL_RADIUS + 20 + 30);
//	//十字
//	ege_line(CENTER_X + DIAL_RADIUS - 9, CENTER_Y + DIAL_RADIUS - 10, CENTER_X + DIAL_RADIUS + 11, CENTER_Y + DIAL_RADIUS - 10);
//	ege_line(CENTER_X + DIAL_RADIUS + 1, CENTER_Y + DIAL_RADIUS - 1, CENTER_X + DIAL_RADIUS + 1, CENTER_Y + DIAL_RADIUS - 19);
//}
